var vue=new Vue({
   el:'#main',
   data:{
       adminInfo:{
           id:''
       },
       roles:[],
       oldRoles:[],
       newRoles:[]
   } ,
    methods:{
       selectOne:function () {
           axios({
               url:`/adminInfo/selectOneByAid/`,//   有/斜杆的是绝对路径，没有的是相对路径
               method:'get',
               params: {
                   Aid:this.adminInfo.id
               }
           }).then(response=>{
               //layer.msg("查询成功");
               this.adminInfo=response.data.obj;
           }).catch(error=>{
               layer.msg(error.message)
           })
       },
        selectRoles:function () {
            axios({
                url:`/role/selectAll`,//   有/斜杆的是绝对路径，没有的是相对路径
            }).then(response=>{
                //layer.msg("查询成功");
                this.roles=response.data.obj;
                for (let i = 0; i < this.roles.length; i++) {
                    Vue.set(this.roles[i],"checked",false);
                }
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        selectRolesByAid:function () {
            axios({
                url:`/role/selectRoleNamesByAid/`,//   有/斜杆的是绝对路径，没有的是相对路径
                method:'get',
                params: {
                    Aid:this.adminInfo.id
                }
            }).then(response=>{
                //layer.msg("查询成功");
                this.oldRoles=response.data.obj;
                this.newRoles=response.data.obj;
                for (let i = 0; i < this.roles.length; i++) {
                    for (let j = 0; j < this.oldRoles.length; j++) {
                        if (this.roles[i].roleId==this.oldRoles[j].roleId){
                            this.roles[i].checked=true;
                        }
                    }
                }
            }).catch(error=>{
                layer.msg(error.message)
            });
        },
        addRole:function (role) {
            for (let i = 0; i < this.roles.length; i++) {
                if (this.roles[i].roleId==role.roleId){
                    this.roles[i].checked=!this.roles[i].checked;
                    if (this.roles[i].checked){
                        this.newRoles.push(this.roles[i]);
                    }
                    else {
                        for (let j = 0; j < this.newRoles.length; j++) {
                            if (this.newRoles[j].roleId==this.roles[i].roleId){
                                this.newRoles.splice(j,1);
                            }
                        }
                    }
                }
            }
        },
        doUpdate:function () {
           if (this.newRoles.length>0){
               axios({
                   url:'/adminInfo/doUpdate',
                   method:'put',
                   data:{
                       Aid:this.adminInfo.adminId,
                       adminInfo:this.adminInfo,
                       roles:this.newRoles
                   }
               }).then(response=>{
                   if (response.data.success){
                       parent.layer.message='success';
                       let index=parent.layer.getFrameIndex(window.name);
                       parent.layer.close(index);
                   }
               }).catch(error=>{
                   layer.msg(error.message);
               })
           }else {
               layer.msg("请至少选择一个角色");
           }
        }
    },
    created:function () {
        this.adminInfo.id=parent.layer.obj;
        this.selectOne();
        this.selectRoles();
        this.selectRolesByAid();
    }
});